<template>
  <view>
<view class="search-box">
  <view class="search" @click="goToSearch">
         <view class="my-search-box">
           <uni-icons type="search" size="17"></uni-icons>
           <text class="placeholder">搜索</text>
         </view>
   </view>
</view>
    <!-- 轮view播图区域 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true" indicator-color="#999" indicator-active-color="#fff">
      <!-- 循环渲染轮播图的 item 项 -->
      <swiper-item v-for="(item, i) in swiperList.message" :key="i">
        <navigator :url="'/subPackge/goods/goods?goods_id=' + item.id" class="swiper-item">
          <!-- 动态绑定图片的 src 属性 -->
          <image :src="item.image_src"></image>
        </navigator>
      </swiper-item>
    </swiper>
    <!-- 分类导航区域 -->
    <view class="nav-list">
       <view class="nav-item" v-for="(item, i) in navList" :key="i" @click="clickNav(item)">
         <image :src="item.image_src" class="nav-img"></image>
       </view>
    </view>
    <!-- 楼层区域 -->
    <view class="floor-list">
      <!-- 楼层 item 项 -->
      <view class="floor-item" v-for="(item, i) in floorList" :key="i">
        <!-- 楼层标题 -->
        <image :src="item.floor_title.image_src" class="floor-title">
        </image>
        <!-- 楼层图片区域 -->
        <view class="floor-img-box">
          <!-- 左侧大图片的盒子 -->
          <navigator class="left-img-box" url="/subPackge/goodsList/goodsList">
            <image :src="item.product_list[0].image_src" :style="{width: item.product_list[0].image_width + 'rpx'}" mode="widthFix"></image>
          </navigator>
          <!-- 右侧 4 个小图片的盒子 -->
          <view class="right-box">
            <navigator  class="right-img-item" v-for="(item2, i2) in item.product_list" :key="i2" :url="item2.url">
              <image v-if="i2 !== 0" :style="{ width: item2.image_width + 'rpx'}"  :src="item2.image_src" mode="widthFix"></image>
            </navigator>
          </view>
        </view>
      </view>
      
    </view>
    
    
    
    
  </view>
  
</template>

<script>
  import {$http} from '@escook/request-miniprogram'
  export default {
    data() {
      return {
       // 这是轮播图的数据列表
       swiperList: {},
       navList: [],
       floorList: []
      };
    },
    onLoad() {
      this.getSwiperList()
      this.getNavList()
      this.getFloorList()
    },
    methods: {
      // 获取轮播图数据
      async  getSwiperList(){
        const res =  await  $http.get('https://api-hmugo-web.itheima.net/api/public/v1/home/swiperdata')
        // 请求失败
        if (res.data.meta.status !== 200) {
          return uni.showToast({
            title: '请求失败',
            duration:1500,
            icon:'none'
          })
        }
        this.swiperList = res.data
        // console.log(this.swiperList.message[0].open_type);
        // console.log(res);
        
      },
      // 获取首页分类数据
      async  getNavList(){
        const res = await $http.get('https://api-hmugo-web.itheima.net/api/public/v1/home/catitems')
        // console.log(res.data.message)
        if (res.data.meta.status !== 200) {
          return uni.showToast({
            title: '获取失败',
            duration: 1500,
            icon: 'none'
          })
        }
        this.navList = res.data.message
        
      }  ,
    
      // 分类导航点击效果
      clickNav(item) {
        // 如果是第一项就跳转到分类页面
        if (item.name === '分类') {
          uni.switchTab({
            url: '/pages/cartegory/cartegory'
          })
        }
      },
    
      // 获取楼层数据
      async getFloorList() {
        const res = await $http.get('https://api-hmugo-web.itheima.net/api/public/v1/home/floordata')
        console.log(res);
       if (res.data.meta.status !== 200) {
         return uni.showToast({
           title: '请求失败',
           duration: 1500,
           icon: 'none'
         })
       }
       
       // 对数据进行处理
       res.data.message.forEach( item => {
         item.product_list.forEach(i => {
           i.url = '/subPackge/goodsList/goodsList?' + i.navigator_url.split('?')[1]
         })
       })
        
        console.log(res)
        this.floorList = res.data.message
      },
    
      goToSearch() {
        uni.navigateTo({
          url: '/subPackge/search/search',
          
        })
      }
    
    
    
    },
     
  }
</script>

<style lang="scss">
swiper {
 height: 330rpx;

 .swiper-item,
 image {
   width: 100%;
   height: 100%;
 }
}
.nav-list {
  display: flex;
  justify-content: space-around;
  margin: 15px 0;

  .nav-img {
    width: 128rpx;
    height: 140rpx;
  }
}
.floor-title {
  height: 60rpx;
  width: 100%;
  display: flex;
}
.right-box {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}
.floor-img-box {
  display: flex;
  margin: 0 10rpx;
  margin-top: 10rpx;
  
}
.search {
  background-color: #35c4f8;
  height: 50px;
  padding: 0 10px;
  display: flex;
  align-items: center;

}

.my-search-box {
  height: 36px;
  background-color: #ffffff;
  border-radius: 15px;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;

  .placeholder {
    font-size: 15px;
    margin-left: 5px;
  }
}
.search-box {
  position: sticky;
  top: 0;
  z-index: 10;
}
</style>
